package com.example.zwg;

import android.content.Intent;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 语音合成
 */
public class MainActivity extends AppCompatActivity {
    private TextToSpeech mTextToSpeech;
    private static int TTS_DATA_CHECK=1;
    private static int VOICE_RECOGNITION=2;
    private boolean ttsIsInit;
    Button mButton,btn,btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
        initData();
    }
    public void initData() {
        //语音合成
        initTextToSpeech();

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speak("");
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //语音识别
                initRecognizer();
            }
        });


        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //语音识别
                initRecognizer2();
            }
        });
    }

    private void initRecognizer() {
        Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"or forever hold your peace");
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.UK);
        startActivityForResult(intent,VOICE_RECOGNITION);
    }
    private void initRecognizer2() {
        Intent intent=new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"or forever hold your peace");
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.UK);
        startActivityForResult(intent,VOICE_RECOGNITION);
    }
    private void initview() {
        mButton=findViewById(R.id.button);
        btn=findViewById(R.id.btn);
        btn2=findViewById(R.id.btn2);
    }

    public void speak(String str) {
        if (mTextToSpeech!=null){
            if (!TextUtils.isEmpty(str)){
                mTextToSpeech.speak(str,TextToSpeech.QUEUE_ADD,null);
            }else{
                mTextToSpeech.speak("我简直无语了，" +
                        "一天到晚就知道嗷嗷叫。天天也不知道嗷嗷的是个啥。" +
                        "简直就是一个母老虎。我现在很烦她，" +
                        "天天把我身上弄得青一块紫一块的，我很生气。",TextToSpeech.QUEUE_ADD,null);
            }
        }
    }
    private void initTextToSpeech(){
        /**
         * 用于检查是否安装语音引擎，
         * 这里推荐讯飞语音引擎比较好用
         */
        Intent intent=new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(intent,TTS_DATA_CHECK);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        /**
         * 语音合成
         */
         if (requestCode==TTS_DATA_CHECK){
             if (resultCode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
                 mTextToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
                     @Override
                     public void onInit(int status) {
                         if (status==TextToSpeech.SUCCESS){
                             ttsIsInit=true;
                             if (mTextToSpeech.isLanguageAvailable(Locale.UK)>=0)
                             mTextToSpeech.setLanguage(Locale.UK);
                             mTextToSpeech.setPitch(0.8f);
                             mTextToSpeech.setSpeechRate(1.1f);
                         }
                     }
                 });
             }else{
                 Intent installVoice=new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                 startActivity(installVoice);
             }
         }
        /**
         * 语音识别
         */
         if(requestCode==VOICE_RECOGNITION&&resultCode==RESULT_OK){
             ArrayList<String> results;
             results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
             float[]  confidence;
             String confidenceExtra=RecognizerIntent.EXTRA_CONFIDENCE_SCORES;
             confidence=data.getFloatArrayExtra(confidenceExtra);

             speak(results.get(0));
         }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTextToSpeech!=null){
            mTextToSpeech.stop();
            mTextToSpeech.shutdown();
        }
    }
}
